#***************************************************************************
#                                  _   _ ____  _
#  Project                     ___| | | |  _ \| |
#                             / __| | | | |_) | |
#                            | (__| |_| |  _ <| |___
#                             \___|\___/|_| \_\_____|
#
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
# SPDX-License-Identifier: curl
#
###########################################################################
# Find the nghttp2 library
# 此模块用于查找系统中的 nghttp2 库。
# 它会尝试通过 pkg-config 或者手动查找的方式定位 nghttp2 库和头文件。

# Input variables:
# 输入变量，用户可手动指定以影响查找过程
# - `NGHTTP2_INCLUDE_DIR`:   The nghttp2 include directory.
#                            nghttp2 头文件所在的目录。
# - `NGHTTP2_LIBRARY`:       Path to `nghttp2` library.
#                            `nghttp2` 库文件的路径。

# Result variables:
# 查找结果变量，查找完成后会被设置相应的值
# - `NGHTTP2_FOUND`:         System has nghttp2.
#                            系统中是否找到了 nghttp2 库。
# - `NGHTTP2_INCLUDE_DIRS`:  The nghttp2 include directories.
#                            nghttp2 头文件所在的目录列表。
# - `NGHTTP2_LIBRARIES`:     The nghttp2 library names.
#                            nghttp2 库文件的名称列表。
# - `NGHTTP2_LIBRARY_DIRS`:  The nghttp2 library directories.
#                            nghttp2 库文件所在的目录列表。
# - `NGHTTP2_PC_REQUIRES`:   The nghttp2 pkg-config packages.
#                            nghttp2 的 pkg-config 包名。
# - `NGHTTP2_CFLAGS`:        Required compiler flags.
#                            编译所需的标志。
# - `NGHTTP2_VERSION`:       Version of nghttp2.
#                            nghttp2 的版本号。

# 设置 nghttp2 的 pkg-config 包名
set(NGHTTP2_PC_REQUIRES "libnghttp2")

# 若启用了使用 pkg-config 查找库，并且未手动指定头文件目录和库文件路径
if(CURL_USE_PKGCONFIG AND
   NOT DEFINED NGHTTP2_INCLUDE_DIR AND
   NOT DEFINED NGHTTP2_LIBRARY)
  # 安静模式查找 PkgConfig 模块，避免输出过多信息
  find_package(PkgConfig QUIET)
  # 使用 pkg-config 检查 nghttp2 库，获取相关信息
  pkg_check_modules(NGHTTP2 ${NGHTTP2_PC_REQUIRES})
endif()

# 若通过 pkg-config 找到了 nghttp2 库
if(NGHTTP2_FOUND)
  # 将 NGHTTP2_CFLAGS 中的分号替换为空格，以符合编译标志的格式要求
  string(REPLACE ";" " " NGHTTP2_CFLAGS "${NGHTTP2_CFLAGS}")
  # 输出找到 nghttp2 库的状态信息，包含头文件目录和版本号
  message(STATUS "Found NGHTTP2 (via pkg-config): ${NGHTTP2_INCLUDE_DIRS} (found version \"${NGHTTP2_VERSION}\")")
else()
  # 手动查找 nghttp2 的头文件目录，查找的头文件为 nghttp2/nghttp2.h
  find_path(NGHTTP2_INCLUDE_DIR NAMES "nghttp2/nghttp2.h")
  # 手动查找 nghttp2 的库文件，尝试查找 nghttp2 和 nghttp2_static 两个库名
  find_library(NGHTTP2_LIBRARY NAMES "nghttp2" "nghttp2_static")

  # 从缓存中移除 NGHTTP2_VERSION 变量，避免旧版本信息影响当前查找
  unset(NGHTTP2_VERSION CACHE)
  # 若找到了头文件目录，并且 nghttp2/nghttp2ver.h 文件存在
  if(NGHTTP2_INCLUDE_DIR AND EXISTS "${NGHTTP2_INCLUDE_DIR}/nghttp2/nghttp2ver.h")
    # 定义正则表达式，用于匹配 NGHTTP2_VERSION 宏定义
    set(_version_regex "#[\t ]*define[\t ]+NGHTTP2_VERSION[\t ]+\"([^\"]*)\"")
    # 从 nghttp2/nghttp2ver.h 文件中读取匹配正则表达式的行
    file(STRINGS "${NGHTTP2_INCLUDE_DIR}/nghttp2/nghttp2ver.h" _version_str REGEX "${_version_regex}")
    # 使用正则表达式提取版本号
    string(REGEX REPLACE "${_version_regex}" "\\1" _version_str "${_version_str}")
    # 设置 NGHTTP2_VERSION 变量为提取的版本号
    set(NGHTTP2_VERSION "${_version_str}")
    # 移除临时变量，避免污染全局变量空间
    unset(_version_regex)
    unset(_version_str)
  endif()

  # 包含 FindPackageHandleStandardArgs 模块，用于统一处理查找结果
  include(FindPackageHandleStandardArgs)
  # 使用标准参数处理查找结果，判断是否找到 nghttp2 库
  find_package_handle_standard_args(NGHTTP2
    REQUIRED_VARS
      NGHTTP2_INCLUDE_DIR
      NGHTTP2_LIBRARY
    VERSION_VAR
      NGHTTP2_VERSION
  )

  # 若手动查找找到了 nghttp2 库
  if(NGHTTP2_FOUND)
    # 设置头文件目录列表
    set(NGHTTP2_INCLUDE_DIRS ${NGHTTP2_INCLUDE_DIR})
    # 设置库文件名称列表
    set(NGHTTP2_LIBRARIES    ${NGHTTP2_LIBRARY})
  endif()

  # 将 NGHTTP2_INCLUDE_DIR 和 NGHTTP2_LIBRARY 标记为高级变量，在 CMake GUI 中默认隐藏
  mark_as_advanced(NGHTTP2_INCLUDE_DIR NGHTTP2_LIBRARY)
endif()
